<bookcatalog>
{
<!-- using a for clause to loop over the book elements in catalog.xml-->
<!-- $index variable yields the position in the sequence that is being processed-->
for $book at $index in doc("catalog.xml")/bookcatalog/section/book
<!-- let clause initialise the variables to be used later -->
let $price := $book/price
let $genre := $book/genre
<!-- where clause filters the books based on thier genre-->
where $genre = "Fantasy"
<!--orders them by their price-->
order by $price descending
<!--return clause creates and returns a new book element with the new index element--> 
return
<!-- here index becomes the attribute of the returned book element-->
<book index = "{$index}">
<!-- includes all the attributes from the original book element-->
{$book/@*}
<!-- includes all the child elements-->
{$book/*}
</book>
}
</bookcatalog>
